package com.blog.util;

import com.blog.model.MailPropertiesModel;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import java.util.Properties;

/**
 * @author jiafujun
 * 2020-06-11 16:24:34
 **/
public class EmailUtil {

    public static String emailTemplate = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"width: 600px; border: 1px solid #ddd; border-radius: 3px; color: #555; \n" +
            "      font-family: 'Helvetica Neue Regular',Helvetica,Arial,Tahoma,'Microsoft YaHei','San Francisco','微软雅黑','Hiragino Sans GB',STHeitiSC-Light; \n" +
            "      font-size: 12px; height: auto; margin: auto; overflow: hidden; text-align: left; word-break: break-all; word-wrap: break-word;\">\n" +
            "    <tbody style=\"margin: 0; padding: 0;\">\n" +
            "    <tr style=\"background-color: #51a0e3; height: 60px; margin: 0; padding: 0;\">\n" +
            "        <td style=\"margin: 0; padding: 0;\">\n" +
            "            <div style=\"color: #ffffff; margin: 0; margin-left: 30px; padding: 0;\">\n" +
            "                <a style=\"font-size: 14px; margin: 0; padding: 0; color: #ffffff; text-decoration: none;\" href=\"http://boyjfj.top/\" target=\"_blank\" rel=\"noopener\">\n" +
            "           Poetry-Blog 博客  评论回复</a>\n" +
            "      </div>\n" +
            "        </td>\n" +
            "    </tr>\n" +
            "    <tr style=\"margin: 0; padding: 0;\">\n" +
            "        <td style=\"margin: 0; padding: 30px;\">\n" +
            "      <p style=\"line-height: 20px; margin: 0; margin-bottom: 10px; padding: 0;\">\n" +
            "            Hi，<em style=\"font-weight: 700;\">${Commentator}$</em>：\n" +
            "      </p>\n" +
            "            <div style=\"\">\n" +
            "        您对文章《<em style=\"font-weight: 700;font-style:normal;\">${article}$</em>》的评论有回复消息：\n" +
            "        <p style=\"line-height: 20px; padding: 10px; background-color: #f2f2f2; font-size: 12px;\">\n" +
            "          <span>基友 <em style=\"font-weight: 700;font-style:normal;\">${reply}$</em>：</span><br/>\n" +
            "          <span style=\"padding-left: 25px;\">${message}$</span>\n" +
            "        </p>\n" +
            "      </div>\n" +
            "      <a href=\"http://boyjfj.top/blog/single/${articleId}$\" style=\"background-color: #51a0e3; color: #fff; display: inline-block; height: 32px; line-height: 32px; margin: 0 15px 0 0; padding: 0 15px; text-decoration: none;\" target=\"_blank\" rel=\"noopener\">前往文章  >></a>\n" +
            "    </td>\n" +
            "    </tr>\n" +
            "    <tr style=\"background-color: #f2f2f2; color: #999; height: 35px; margin: 0; padding: 0; text-align: center;\">\n" +
            "        <td style=\"margin: 0; padding: 0;\">系统邮件，请勿直接回复。</td>\n" +
            "    </tr>\n" +
            "    </tbody>\n" +
            "</table>";

    /**
     * 初始化邮件配置
     *
     * @return
     */
    public static JavaMailSenderImpl getJavaMailSender(MailPropertiesModel model) {
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        javaMailSender.setHost(model.getHost());
        javaMailSender.setPort(model.getPort());
        javaMailSender.setUsername(model.getUsername());
        javaMailSender.setPassword(model.getPassword());
        javaMailSender.setDefaultEncoding("utf-8");
        Properties properties = new Properties();
        properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        javaMailSender.setJavaMailProperties(properties);
        return javaMailSender;
    }
}
